package com.feng.financial.manager.controller;

import com.feng.financial.common.exception.ErrorCode;
import com.feng.financial.common.exception.ValidationException;
import com.feng.financial.common.querydsl.FetchResponse;
import com.feng.financial.common.web.Range;
import com.feng.financial.common.web.WebCommonResponse;
import com.feng.financial.common.web.WebCommonResponseBuilder;
import com.feng.financial.manager.service.ProductService;
import com.feng.financial.manager.vo.ProductSaveVo;
import com.feng.financial.model.ProductEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author Administrator
 */
@Api(description = "产品相关",tags = "tags")
@RequestMapping("/product")
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("save")
    public WebCommonResponse<ProductEntity> save(@RequestBody @Valid ProductEntity product){
        product = productService.saveProduct(product);
        return new WebCommonResponseBuilder<ProductEntity>().success().build(product);
    }

    @ApiOperation(value = "分页查询所有理财产品",notes = "分页查询所有理财产品")
    @GetMapping("findAllProduct")
    public WebCommonResponse<FetchResponse<ProductEntity>> findAllProduct(Range range){
        FetchResponse<ProductEntity> response = productService.findAllByPage(range);
        return new WebCommonResponseBuilder<FetchResponse<ProductEntity>>().success().build(response);
    }

    @PostMapping("saveTest")
    public WebCommonResponse<ProductEntity> saveTest(@RequestBody @Valid ProductSaveVo productSaveVo) throws ValidationException {
        throw new ValidationException(ErrorCode.PARAM_ERROR.getStatusCode(), "就想抛出异常");
    }
}
